//
//    JPianinho.java
//    http://code.google.com/p/jpianinho/
//
//    Created on 16 de Maio de 2006, 08:19
//    Copyright (C) 2006  Felix Farias
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation version 2.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License along
//    with this program; if not, write to the Free Software Foundation, Inc.,
//    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

package jpianinho;

// imports
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;


// class JPianinho
class JPianinhoCanvas extends Canvas {
    private int note,tone,key,accident,tonic,duration;
    private boolean major;
    
    // class constructor
    public JPianinhoCanvas() {
        // initialize vars
        note = 0;
        tone = 0;
        key = 0;
        accident = 0;
        major = true;
        tonic = 0;
        duration=500;
    }
    
    // paint procedure
    public void paint(Graphics g) {
        // get screen size
        int w = getWidth();
        int h = getHeight();
        
        // clear screen
        g.setColor(0xffffff);
        g.fillRect(0, 0, w, h);
        g.setColor(0x000000);
        
        // draw screen
        //g.drawString("JPianinho 0.12",10,10,0);
        //g.drawString("=======================",10,20,0);
        g.drawString("piano keys 1-9",10,10,0);
        if(major){
            g.drawString("* = Major",10,25,0);
        } else {
            g.drawString("* = minor",10,25,0);
        }
        g.drawString("0=Octave "+Integer.toString(tone),10,40,0);
        g.drawString("#=Accident "+Integer.toString(accident),10,55,0);
        g.drawString("U/D=Tonic "+Integer.toString(tonic),10,70,0);
        g.drawString("L/R=Duration "+Integer.toString(duration),10,85,0);
    }
    
    // key pressed handler
    public void keyPressed(int code) {
        // play now?
        boolean play = true;
        
        // key alias reference:
        // KEY_NUM0 (0), KEY_NUM1 (1), KEY_NUM2 (2), ...
        // KEY_STAR (*) and KEY_POUND (#)
        
        key = code;
        // find and execute required action (play or config)
        if(key>0){
        switch (key) {
            case KEY_NUM1:
                note = 60 + tone + accident + tonic;
                repaint();
                break;
            case KEY_NUM2:
                note = 62 + tone + accident + tonic;
                repaint();
                break;
            case KEY_NUM3:
                // if not major tone, use minor third
                if (!major) {
                    accident = -1;
                }
                note = 64 + tone + accident + tonic;
                repaint();
                break;
            case KEY_NUM4:
                note = 65 + tone + accident + tonic;
                repaint();
                break;
            case KEY_NUM5:
                note = 67 + tone + accident + tonic;
                repaint();
                break;
            case KEY_NUM6:
                // if not major tone, use minor sixth
                if (!major) {
                    accident = -1;
                }
                note = 69 + tone + accident + tonic;
                repaint();
                break;
            case KEY_NUM7:
                if (!major) {
                    accident = -1;
                }
                note = 71 + tone + accident + tonic;
                repaint();
                break;
            case KEY_NUM8:
                note = 72 + tone + accident + tonic;
                repaint();
                break;
            case KEY_NUM9:
                note = 74 + tone + accident + tonic;
                repaint();
                break;
            case KEY_STAR:
                major = !major;
                play = false;
                repaint();
                break;
            case KEY_NUM0:
                if (tone==0) {
                    tone = 12;
                } else {
                    tone = 0;
                }
                play = false;
                repaint();
                break;
            case KEY_POUND:
                accident = accident + 1;
                if (accident>1) {
                    accident = -1;
                }
                play = false;
                repaint();
                break;
            default:
                play = false;
                break;
        }
        }
        else {
            key=getGameAction(code);
            play = false;
        switch (key) {
            case UP:
                tonic = tonic + 1;
                repaint();
                break;
            case DOWN:
                tonic = tonic - 1;
                repaint();
                break;
            case LEFT:
                duration = duration-100;
                if (duration < 100) {
                    duration = 100;
                }
                repaint();
                break;
            case RIGHT:
                duration = duration+100;
                if (duration > 5000) {
                    duration = 5000;
                }
                repaint();
                break;
            default:
                break;
        }            
            
        }
        
        // play if key pressed was not a config key
        if (play) {
            try {
                // play note for 1000 ms at volume 100
                Manager.playTone(note, duration, 100);
            } catch (MediaException e) {
            }
            // nor sucessive accidents by default
            accident = 0;
        }
    }
}
